package cn.baage.campus.service.impl;

import cn.baage.campus.enums.ExceptionType;
import cn.baage.campus.exception.BizException;
import cn.baage.campus.mapper.CategoryMapper;
import cn.baage.campus.model.po.Category;
import cn.baage.campus.service.CategoryService;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description: 分类Service实现
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    /**
     * 新增分类
     *
     * @param category 分类
     */
    @Override
    public void addCategory(Category category) {
        String id = IdUtil.simpleUUID();
        category.setId(id);
        Date now = new Date();
        category.setCreateTime(now);
        category.setUpdateTime(now);
        this.baseMapper.insert(category);
    }


    /**
     * 修改分类
     *
     * @param category 分类
     */
    @Override
    public void updateCategory(Category category) {
        Category categoryByDb = this.baseMapper.selectById(category.getId());
        if (categoryByDb == null) {
            throw new BizException(ExceptionType.PARAM_NOT_VALID);
        }
        categoryByDb.setUpdateTime(new Date());
        categoryByDb.setName(category.getName());
        categoryByDb.setOrderNum(category.getOrderNum() < 0 ? 0 : category.getOrderNum());
        this.baseMapper.updateById(categoryByDb);
    }


}
